package com.example.lovewordsapplication;

import android.app.Application;
import android.content.Context;
import android.content.SharedPreferences;
import android.content.pm.PackageInfo;
import android.content.pm.PackageManager;

import cn.bmob.v3.Bmob;

/**
 * @author Leung
 * @date 2020/4/4 22:45
 */
public class MyApplication extends Application {

    private static MyApplication instance;

    public static synchronized MyApplication getInstance() {
        return instance;
    }

    private SharedPreferences config;

    public static final int LOGIN_REQUEST_CODE = 1;
    public static final int REGISTER_REQUEST_CODE = 2;
    public static final int FORGET_PASSWORD_REQUEST_CODE = 3;

    /**
     * 上下文
     */
    private static Context context;

    /**
     * 获取全局上下文
     *
     * @return 上下文
     */
    public Context getContext() {
        return context;
    }

    @Override
    public void onCreate() {
        super.onCreate();
        instance = this;
        context = instance.getApplicationContext();
        // 初始化Bmob
        Bmob.initialize(this, "061a781170802ab430a260db8f92e238");
        // 获取本地信息
        config = getSharedPreferences("app_config", MODE_PRIVATE);
    }

    /**
     * 是否是网络获取
     *
     * @return 是否是网络获取
     */
    public boolean isNetWork() {
        return config.getBoolean("settings_network", false);
    }

    /**
     * 设置网络获取
     *
     * @param isNetWork 是否是网络获取
     */
    public void setNetWork(boolean isNetWork) {
        SharedPreferences.Editor editor = getSharedPreferences("app_config", MODE_PRIVATE).edit();
        editor.putBoolean("settings_network", isNetWork);
        editor.apply();
    }

    /**
     * 获取版本名称
     *
     * @return 版本名称
     */
    public String getVersion() {
        PackageManager packageManager = getPackageManager();
        try {
            PackageInfo info = packageManager.getPackageInfo(getPackageName(), 0);
            return info.versionName;
        } catch (PackageManager.NameNotFoundException e) {
            return e.getMessage();
        }
    }

}
